package com.ey.controller;

import com.ey.pojo.EducationInfo;
import com.ey.pojo.PersonalInfo;
import com.ey.service.WordService;
import com.ey.util.AliyunOOSUtil;
import com.ey.util.DownloadFileUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by Corazono on 2018/10/11.
 */
@RestController
@RequestMapping("/word")
public class WordController {
    @Autowired
    private AliyunOOSUtil aliyunOOSUtil;
    @Autowired
    private WordService wordService;

    @RequestMapping("/getWord")
    public void getWord(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("调用方法");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/msword");
        DownloadFileUtil.setResponseHeader(response, "测试.doc");
        // 创建一个FreeMarker实例, 负责管理FreeMarker模板的Configuration实例
        Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        // 指定FreeMarker模板文件的位置
        cfg.setClassForTemplateLoading(getClass(), "/templates");
        Template template = null;
        try {
            template = cfg.getTemplate("resume_model_01.ftl", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String, Object> root = wordService.getWord();
        try {
            template.process(root, new OutputStreamWriter(response.getOutputStream()));
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成简历
     * @param education
     * @param photo
     * @param comments
     */
    @RequestMapping("/mkResume")
    public void makeResume(HttpServletRequest request, HttpServletResponse response,EducationInfo education,
                           MultipartFile photo, String comments) {
        // 阿里云OOS上传图片
        String fileUrl = "";
        if (photo != null) {
            // 访问路径 ：个人的OOS访问路径 + 上传方法返回的文件名
            fileUrl = AliyunOOSUtil.OOSBaseUrl + UploadToOOS(photo);
            System.out.println(fileUrl);
        }
        // 设置个人信息对象
        PersonalInfo personal = new PersonalInfo();
        personal.setPhotoPath(fileUrl);
        // 自我评价用、号分割 封装成list
        String[] split = comments.split("、");
        List<String> self_comments = Arrays.asList(split);
        personal.setComments(self_comments);

        // 开始生成word文档
        getWord(request,response,education,personal);
    }

    /**
     * 前端下载简历
     * @param request
     * @param response
     * @param education
     * @param person
     */
    public void getWord(HttpServletRequest request, HttpServletResponse response, EducationInfo education, PersonalInfo person) {
        System.out.println("调用方法");
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/msword");
        DownloadFileUtil.setResponseHeader(response, "我的简历.doc");
        // 创建一个FreeMarker实例, 负责管理FreeMarker模板的Configuration实例
        Configuration cfg = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        // 指定FreeMarker模板文件的位置
        cfg.setClassForTemplateLoading(getClass(), "/templates");
        Template template = null;
        try {
            template = cfg.getTemplate("resume_model_01.ftl", "UTF-8");
        } catch (IOException e) {
            e.printStackTrace();
        }

        Map<String, Object> root = wordService.getWord(education,person);
        try {
            template.process(root, new OutputStreamWriter(response.getOutputStream()));
        } catch (TemplateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String UploadToOOS(MultipartFile file) {
        System.out.println("=========>文件上传");
//        log.info("=========>文件上传");
        String uploadUrl = "";
        try {
            String filename = file.getOriginalFilename();
            if (!"".equals(filename.trim())) {
                File newFile = new File(filename);
                FileOutputStream os = new FileOutputStream(newFile);
                os.write(file.getBytes());
                os.close();
                file.transferTo(newFile);
                //上传到OSS
                uploadUrl = aliyunOOSUtil.upload(newFile);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return uploadUrl;
    }
}
